package org.martin.snsrs.actions;

import java.util.Date;

import org.martin.snsrs.beans.UserComment;
import org.martin.snsrs.daos.ActivityDao;
import org.martin.snsrs.services.UserServices;

public class AddUserComment extends UserBase{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7991095371800508081L;
	
	private String sessionKey;
	
	private Integer activityID;
	
	private String comment;
	
	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	private Integer rate;
	
	private UserServices userServices;

	public Integer getActivityID() {
		return activityID;
	}

	public void setActivityID(Integer activityID) {
		this.activityID = activityID;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Integer getRate() {
		return rate;
	}

	public void setRate(Integer rate) {
		this.rate = rate;
	}

	public ActivityDao getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}

	private ActivityDao activityDao;
	
	private boolean isValidate;
	
	public void validate(){
		if(sessionKey == null){
			status = "error";
			errorReason = "未知用户";
			isValidate = false;
			return;
		}
		if(activityID == null){
			status = "error";
			errorReason = "未知活动";
			isValidate = false;
			return;
		}
		if(comment == null){
			status = "error";
			errorReason = "评论为空";
			isValidate = false;
			return;
		}
		if(rate == null){
			status = "error";
			errorReason = "评分为空";
			isValidate = false;
			return;
		}
		if(userServices.getUserBySessionKey(sessionKey) == null){
			status = "error";
			errorReason = "未知用户";
			isValidate = false;
			return;
		}
		if(activityDao.getActivity(activityID) == null){
			status = "error";
			errorReason = "未知活动";
			isValidate = false;
			return;
		}
	}
	
	public String execute(){
		if(!isValidate){
			return ERROR;
		}
		UserComment userComment = new UserComment();
		userComment.setActivity(activityDao.getActivity(activityID));
		userComment.setComment(comment);
		userComment.setDate(new Date());
		userComment.setRate(rate);
		userComment.setUser(userServices.getUserDao().getUser(userServices.getUserBySessionKey(sessionKey)));	
		activityDao.saveUserComment(userComment);
		return SUCCESS;
	}
}
